package com.rxyb.redis.utils;

import com.rxyb.redis.constant.RedisConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis list类型工具类
 *
 * @author duyh
 * @date 2020/6/5
 */
@Slf4j
@Service
public class RedisListUtils {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 将list放入缓存(右侧)
     *
     * @param key
     * @param value
     * @return
     */
    public void rset(String group, String key, List<Object> value) {
        try {
            redisTemplate.opsForList().rightPushIfPresent(RedisConstant.REDIS_LIST + "_" + group + "_" + key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将list放入缓存(左侧)
     *
     * @param key
     * @param value
     */
    public void lset(String group, String key, List<Object> value) {
        try {
            redisTemplate.opsForList().leftPushIfPresent(RedisConstant.REDIS_LIST + "_" + group + "_" + key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将list放入缓存(左侧)
     *
     * @param key
     * @param value
     */
    public boolean lset(String group, String key, List<Object> value, TimeUnit timeUnit, long times) {
        try {
            redisTemplate.opsForList().leftPushIfPresent(RedisConstant.REDIS_LIST + "_" + group + "_" + key, value);
            RedisObjectUtils redisObjectUtils = new RedisObjectUtils();
            return redisObjectUtils.setExpire(key, timeUnit, times);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 将list放入缓存(左侧)
     *
     * @param key
     * @param value
     */
    public boolean lset(String key, List<Object> value, TimeUnit timeUnit, long times) {
        try {
            redisTemplate.opsForList().leftPushIfPresent(key, value);
            RedisObjectUtils redisObjectUtils = new RedisObjectUtils();
            return redisObjectUtils.setExpire(key, timeUnit, times);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据key获取redis的值
     *
     * @param key
     * @return
     */
    public Optional<Object> get(String group, String key) {
        Object object = redisTemplate.opsForValue().get(RedisConstant.REDIS_LIST + "_" + group + "_" + key);
        if (null == object) {
            return Optional.empty();
        }
        return Optional.of(object);
    }

    /**
     * 获取list缓存的长度
     *
     * @param key
     * @return
     */
    public long getSize(String key) {
        return redisTemplate.opsForList().size(key);
    }

    /**
     * 通过key和索引获取列表中的元素
     *
     * @param key
     * @param index
     * @return
     */
    public Optional<String> getKeyByIndex(String key, long index) {
        String s = redisTemplate.opsForList().index(key, index).toString();
        if (null == s) {
            return Optional.empty();
        }
        return Optional.of(s);
    }

    /**
     * 获取列表指定范围内的元素
     *
     * @param key
     * @param start 0开始位置
     * @param end   结束位置 0到-1返回所有
     * @return
     */
    public Optional<List<Object>> getList(String key, long start, long end) {
        List list = redisTemplate.opsForList().range(key, start, end);
        if (null == list) {
            return Optional.empty();
        }
        return Optional.of(list);
    }

    /**
     * 从存储在键
     * count>0删除等于从左到右移动的值得第一个元素
     * count<0删除等于从右到左移动的值得第一个元素
     * count=0删除等于value的所有元素
     *
     * @param key
     * @param count
     * @param value
     * @return 移除元素数量
     */
    public long del(String key, long count, String value) {
        return redisTemplate.opsForList().remove(key, count, value);
    }

    /**
     * 根据key删除redis
     *
     * @param key
     */
    public void del(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 根据key删除redis
     *
     * @param key
     */
    public void del(String group, String key) {
        redisTemplate.delete(RedisConstant.REDIS_LIST + "_" + group + "_" + key);
    }

    /**
     * 根据key删除redis
     *
     * @param group
     */
    public void delgroup(String group) {
        //最后一定要带上 *
        Set<String> keys = redisTemplate.keys(RedisConstant.REDIS_LIST + "_" + group + "_*");
        redisTemplate.delete(keys);
    }

}
